<?php
class XmlParser extends Object
{
	public static function Xml2array($xml)
	{
		$xmlary = array();
		$reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
		$reattrs = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
		preg_match_all($reels, $xml, $elements);
		foreach ($elements[1] as $ie => $xx)
		{
		    $xmlary[$ie]["name"] = $elements[1][$ie];

		    if ($attributes = trim($elements[2][$ie]))
		    {
		        preg_match_all($reattrs, $attributes, $att);
		        foreach ($att[1] as $ia => $xx)
		            $xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
		    }

		    $cdend = strpos($elements[3][$ie], "<");
		    if ($cdend > 0)
		        $xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);

		    if (preg_match($reels, $elements[3][$ie]))
		        $xmlary[$ie]["elements"] = self::Xml2array($elements[3][$ie]);
		    else if ($elements[3][$ie])
		        $xmlary[$ie]["text"] = $elements[3][$ie];
		}
		return $xmlary;
	}

    public static function ParseTestData($file)
	{
		$array = XmlParser::Xml2array(FileHandler::Read($file));
		$data = self::Format($array[0]);
		return $data['test'];
	}
	
	public static function Format($node)
	{
		$out = null;
		if (isset($node['attributes']))
			foreach ($node['attributes'] as $akey => $avalue)
				$out[$akey] = $avalue;
		if (isset($node['elements']))
			foreach ($node['elements'] as $element)
			{
				$evalue = self::Format($element);
				if (isset($out['type']) && $out['type'] == 'list')
					$out[$element['name']][] = $evalue;
				else
					$out[$element['name']] = $evalue;
			}
		return $out;
	}
}
?>
